import { reactive } from "vue"
export function useGetData(fn: Function) {
    const pageData = reactive({
        data: [],
        searchParams: {},
        loading: true,
        pageInfo: {
            page: 1,
            size: 10,
            total: 0
        }
    })
    //获取数据
    const getData = (params?: {}) => {
        fn({ page: pageData.pageInfo.page, size: pageData.pageInfo.size, ...params })
            .then((res: any) => {
                pageData.data = res.data
                pageData.pageInfo.total = res.total
                pageData.loading = false
            })
            .catch((err: any) => {
                pageData.loading = false
            })
    }
    getData()

    //查询
    const handleSearch = (row: any) => {
        getData({ ...row })
    }
    //重置
    const handleReset = () => {
        getData({})
    }

    return {
        pageData,
        getData,
        handleSearch,
        handleReset
    }
}
